home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 43
/
Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso
/
-serious-
/
comms
/
other
/
ftransapiv
/
sourcecode
/
ftrans.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-06-14
|
10KB
|
341 lines
#include "surround.h"
#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/IntuitionBase.h>
#include <intuition/classusr.h>
#include <intuition/pointerclass.h>
#include <intuition/gadgetclass.h>
#include <intuition/sghooks.h>
#include <intuition/icclass.h>
#include <graphics/displayinfo.h>
#include <graphics/text.h>
#include <graphics/gfx.h>
#include <libraries/dos.h>
#include <libraries/gadtools.h>
#include <libraries/diskfont.h>
#include <libraries/asl.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include <clib/graphics_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/asl_protos.h>
#include <clib/datatypes_protos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos/dos.h>
#include <dos/dosasl.h>
#include <exec/exec.h>
#include <proto/exec.h>
#include <proto/utility.h>
#include <proto/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/pictureclass.h>
#include <datatypes/animationclass.h>
#include <datatypes/datatypes.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <proto/socket.h>
#include <signal.h>
// INFORMACOES DA VERSAO
#define VERS "Freedom Internet Translator Client API v1.0"
#define DATE "01.05.1999"
#define VSTRING VERS " (" DATE ")"
#define VERSTAG "\\0$VER: " VERS " (" DATE ")"
const static char VersTag[] = VERSTAG;
// NUMERO MAXIMO DE SOCKETS ABERTOS
#define MAXSOCKS 10L
// TAMANHO DO BUFFER DE LEITURA DE COMUNICACAO
int TempBufferSize=65535;
// PONTO DE ACESSO A UM HOST
struct hostent *hptr;
struct sockaddr_in server;
int byteslidos,result,totalbytes;
unsigned char *enderecoip;
BPTR ArquivoCache=NULL;
char *TempBuffer=NULL;
char TempString[256], status[256];
LONG TipoEndereco;
char NomeDoHost[256];
char direcao[50];
int socket1;
void main(argc, argv)
int argc;
char *argv[];
{
char *linha, *linhaf, *requisicao,
opcao[10], lingua[10], tamanho[50], a,
*saida, *teste, tmpfile[60];
int x = 0, silence = 0;
FILE *entrada;
// ACERTA O NOME DO ARQUIVO TEMPORARIO
sprintf(tmpfile, "Ram:FreeTmp.%s", DataHora(7));
// TESTA OPCAO SILENCE
if(!(strcmp(strlwr(argv[3]), "silence")) || !(strcmp(strlwr(argv[4]), "silence")))
silence = 1;
// TITULO DO PROGRAMA
if(silence == 0)
{
printf("\nFreedom Internet Translator API v1.0 [01.05.1999]\n");
printf("By Cristian Robert Gallas (SuRRouND)\n");
printf("Lithium Software 1999(r)\n");
printf("FREEWARE WITH SOURCE FREE - CONTRIBUTE TO AMIGA OPEN SOURCE!\n\n");
} else {
printf("\n"); }
strcpy(opcao, argv[1]);
if(!strcmp(strlwr(opcao), "help"))
{
printf("Sintax:\nFTranslator [FILE|TEXT|HELP] [DIRECTION] \"text or txtfile\" [SILENCE]\n\n");
printf("DIRECTION: 0 - English to Francais\n");
printf(" 1 - English to Deutsch\n");
printf(" 2 - English to Italiano\n");
printf(" 3 - English to Portugues\n");
printf(" 4 - English to Espanol\n");
printf(" 5 - Francais to English\n");
printf(" 6 - Deutsch to English\n");
printf(" 7 - Italiano to English\n");
printf(" 8 - Espanol to English\n");
printf(" 9 - Portugues to English\n\n");
exit(0);
}
if(argc < 3)
{
printf("Sintax:\nFTranslator [FILE|TEXT|HELP] [DIRECTION] \"text or txtfile\" [SILENCE]\n\n");
exit(0);
}
// VERIFICA A LINGUA A TRADUZIR
switch (atoi(argv[2]))
{
case 0: strcpy(lingua, "en_fr");
strcpy(direcao, "English to Francais"); break;
case 1: strcpy(lingua, "en_de");
strcpy(direcao, "English to Deutsch"); break;
case 2: strcpy(lingua, "en_it");
strcpy(direcao, "English to Italiano"); break;
case 3: strcpy(lingua, "en_pt");
strcpy(direcao, "English to Portugues"); break;
case 4: strcpy(lingua, "en_es");
strcpy(direcao, "English to Espanol"); break;
case 5: strcpy(lingua, "fr_en");
strcpy(direcao, "Francais to English"); break;
case 6: strcpy(lingua, "de_en");
strcpy(direcao, "Deutsch to English"); break;
case 7: strcpy(lingua, "it_en");
strcpy(direcao, "Italiano to English"); break;
case 8: strcpy(lingua, "es_en");
strcpy(direcao, "Espanol to English"); break;
case 9: strcpy(lingua, "pt_en");
strcpy(direcao, "Portugues to English"); break;
}
// ROTINA QUE LE O CONTEUDO DO TEXT
if(!strcmp(strlwr(opcao), "text"))
{
// ALOCA MEMORIA PARA AS VARIAVEIS DE PEDIDO
x = (strlen(argv[3]) + 500);
linhaf = malloc(x);
linha = malloc(x);
requisicao = malloc(x);
linha[0] = '\0'; requisicao[0] = '\0';
strcpy(linhaf, argv[3]);
x = strlen(argv[3]);
linhaf[x] = '\0';
}
// ROTINA QUE LE O CONTEUDO DE UM ARQUIVO TX
if(!strcmp(strlwr(opcao), "file"))
{
// ROTINA QUE LE O ARQUIVO E JOGA NA VARIAVEL LINHA
if(!(entrada = fopen(argv[3], "r")))
{
printf("%s: Access denied or file not found...\n\n", argv[3]);
exit(0);
}
// ALOCA MEMORIA PARA AS VARIAVEIS DE PEDIDO
x = (atoi(ExaminaArquivo(argv[3], 0)) + 500);
linhaf = malloc(x);
linha = malloc(x);
requisicao = malloc(x);
linhaf[0] = '\0'; linha[0] = '\0'; requisicao[0] = '\0';
// LE O ARQUIVO E JOGA NA VARIAVEL PARA TRADUCAO
x = 0;
while(a = getc(entrada),! feof(entrada))
linhaf[x++] = a;
linhaf[x] = '\0';
fclose(entrada);
}
// INICIA A CONECCAO NO SERVIDOR ABRINDO A SOCKET
SockBase=(struct Library *)OpenLibrary("socket.library",0L);
if(!(SockBase))
{
printf("\nYou need connect on internet first...\n\n");
exit(0);
}
if(silence == 0)
printf("Open Socket...\n");
setup_sockets(MAXSOCKS,&errno);
// PEGA INFORMACOES SOBRE O HOST
if(!(hptr=gethostbyname("babelfish.altavista.digital.com")))
{
printf("\nProblem on translator server, try again later...\n\n");
remove(tmpfile);
goto SAIDAGOTO;
}
memcpy((char *)&server.sin_addr,(char *)hptr->h_addr,hptr->h_length);
// AF_INET = INTERNET
server.sin_family=AF_INET;
// PORTA 80 (SERVIDOR WEB)
server.sin_port=80;
TipoEndereco=hptr->h_addrtype;
strcpy(NomeDoHost,hptr->h_name);
enderecoip=(unsigned char *)hptr->h_addr_list[0];
socket1=socket(hptr->h_addrtype, SOCK_STREAM,0);
if(silence == 0)
printf("Contacting host...\n");
// CONECTA O SOCKET AO ENDERECO
result=connect(socket1,(struct sockaddr*)&server,sizeof(server));
if(result == -1)
{
printf("\nConnection problem, try again later...\n\n");
goto SAIDAGOTO;
}
if(silence == 0)
printf("Host contacted. Now Doing request...\n");
// ROTINA QUE MONTA O PEDIDO DE TRADUCAO
sprintf(linha, "doit=done&urltext='%s'&languagepair=%s", linhaf, lingua);
sprintf(tamanho, "Content-Length: %d", strlen(linha));
// INICIALIZA A REQUISICAO
requisicao[0]='\0';
strcpy(requisicao,"POST /cgi-bin/translate? HTTP/1.0");
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao,"User-Agent: FreedomTranslator/1.0; (Amiga Translator Client)");
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao,"Accept: */*;q=1");
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao,"Host: babelfish.altavista.digital.com");
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao, tamanho);
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao,"Content-Type: application/x-www-form-urlencoded");
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao, linha);
strcat(requisicao,"\x0d\x0a\0");
strcat(requisicao,"\x0d\x0a\0");
// ENVIA A REQUISICAO DE HTTP
result=send(socket1,requisicao,strlen(requisicao),0);
if(silence == 0)
printf("Done http request. Waiting... ");
totalbytes=0;
if(!(ArquivoCache = Open(tmpfile, MODE_NEWFILE)))
{
printf("\nFTranslator port is open, binary FTranslator is in use, wait...\n\n");
goto SAIDAGOTO;
}
TempBuffer=AllocMem(TempBufferSize,MEMF_FAST|MEMF_CLEAR);
if(!(TempBuffer))
{
printf("Not have memory for allocate temporary buffer...\n");
remove(tmpfile);
goto SAIDAGOTO;
}
do
{
byteslidos = 0;
*TempBuffer = '\0';
byteslidos = recv(socket1, TempBuffer, TempBufferSize-1,0);
if(byteslidos)
{
Write(ArquivoCache, TempBuffer, byteslidos);
totalbytes += byteslidos;
TempString[0] = '\0';
}
}
while(byteslidos>0);
// FECHA O SOCKET E O ARQUIVO
s_close(socket1);
Close(ArquivoCache);
FreeMem(TempBuffer,TempBufferSize);
cleanup_sockets();
CloseLibrary((struct Library *)SockBase);
// ETAPA FINAL DO PROGRAMA
if(silence == 0)
printf("Done.\n");
if(!(ExisteArquivo(tmpfile)))
{
printf("%s: Problem on temporary file...\n\n", tmpfile);
exit(0);
}
saida=malloc(atoi(ExaminaArquivo(tmpfile, 0))+500);
teste=malloc(atoi(ExaminaArquivo(tmpfile, 0))+500);
strcpy(saida, CortaStringArquivo(tmpfile, "<font face=\"arial, helvetica\">", "<br><br></font></td></tr>", 2));
// FILTRA LINHA PARA MELHORAR A SAIDA DA TRADUCAO
if(silence == 0)
printf("Applying filters... ");
strcpy(teste, SubstituiString(saida, "<P>", "\n"));
strcpy(saida, teste);
strcpy(teste, SubstituiString(saida, "<", ""));
strcpy(saida, teste);
strcpy(teste, SubstituiString(saida, ">", ""));
strcpy(saida, teste);
strcpy(teste, SubstituiString(saida, "`", " "));
strcpy(saida, teste);
strcpy(teste, SubstituiString(saida, "'", " "));
strcpy(saida, teste);
strcpy(teste, SubstituiString(saida, "\"", " "));
strcpy(saida, teste);
if(silence == 0)
printf("Done.\n\n");
// MOSTRA A TRADUCAO
printf(" Translation: %s\n", direcao);
printf("o----------------------------------------------o\n");
printf("%s\n", saida);
printf("o----------------------------------------------o\n");
printf(" FTranslator API by Cristian Robert Gallas v1.0 \n");
printf(" Freeware with Source Free (Amiga Open Source) \n");
printf("o----------------------------------------------o\n");
// FINALIZA O PROGRAMA
SAIDAGOTO:
remove(tmpfile);
exit(0);
}